package View;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
//学生登录界面
public class StudentLogin extends JFrame {
    String a;
    JFrame jf = new JFrame();
    ButtonGroup bg = new ButtonGroup();
    ButtonGroup bg1 = new ButtonGroup();
    ButtonGroup bg2 = new ButtonGroup();
    JRadioButton y = new JRadioButton("优秀", true);
    JRadioButton l = new JRadioButton("良好", false);
    JRadioButton yi = new JRadioButton("一般", false);


    JRadioButton y1 = new JRadioButton("优秀", true);
    JRadioButton l1 = new JRadioButton("良好", true);
    JRadioButton yi1 = new JRadioButton("一般", true);

    JRadioButton y2 = new JRadioButton("优秀", true);
    JRadioButton l2 = new JRadioButton("良好", true);
    JRadioButton yi2 = new JRadioButton("一般", true);
    JButton tj = new JButton("提交评价");
    JLabel ts = new JLabel();


    public StudentLogin(String a) {
        jf.setSize(333, 333);
        Box b = Box.createVerticalBox();
        Box x = Box.createHorizontalBox();
        Box x1 = Box.createHorizontalBox();
        JLabel km = new JLabel("科目");
        JLabel pj = new JLabel("评价");

        JLabel ja = new JLabel("面向对象程序设计:");

        Box x2 = Box.createHorizontalBox();
        JLabel py = new JLabel("Python程序设计:    ");

        Box x3 = Box.createHorizontalBox();
        JLabel c = new JLabel("C语言:    ");

        Box x4 = Box.createHorizontalBox();
//        JButton ck=new JButton("查看评价结果");
        Box x5 = Box.createHorizontalBox();
        JLabel bnm = new JLabel("注：本次评价不匿名且提交后不可更改");
        bnm.setForeground(Color.blue);

        Box x6 = Box.createHorizontalBox();
        Box x7= Box.createHorizontalBox();
        JButton qh=new JButton("切换账号");
        JButton tc=new JButton("退出系统");


        x1.add(km);
        x1.add(Box.createHorizontalStrut(111));
        x1.add(pj);


        bg.add(y);
        bg.add(l);
        bg.add(yi);
        bg1.add(y1);
        bg1.add(l1);
        bg1.add(yi1);
        bg2.add(y2);
        bg2.add(l2);
        bg2.add(yi2);
        x.add(ja);
        x.add(y);
        x.add(l);
        x.add(yi);
        x2.add(py);
        x2.add(y1);
        x2.add(l1);
        x2.add(yi1);
        x3.add(c);
        x3.add(Box.createHorizontalStrut(58));
        x3.add(y2);
        x3.add(l2);
        x3.add(yi2);
        x4.add(tj);
//        x4.add(Box.createHorizontalStrut(58));x4.add(ck);
        x5.add(bnm);
        x6.add(ts);
        x7.add(qh);x7.add(Box.createHorizontalStrut(30));x7.add(tc);



        b.add(x1);
        b.add(Box.createVerticalStrut(10));
        b.add(x);
        b.add(Box.createVerticalStrut(8));
        b.add(x2);
        b.add(Box.createVerticalStrut(6));
        b.add(x3);
        b.add(Box.createVerticalStrut(8));
        b.add(x4);
        b.add(Box.createVerticalStrut(8));
        b.add(x5);
        b.add(Box.createVerticalStrut(18));
        b.add(x6);
        b.add(Box.createVerticalStrut(10));
        b.add(x7);

        jf.add(b);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        jf.setResizable(false);
        jf.setVisible(true);
        jf.setLayout(new FlowLayout(FlowLayout.CENTER));
        qh.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Login().zb();
                jf.dispose();
            }
        });
        tc.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection co = DriverManager.getConnection("jdbc:mysql://localhost:3306/lxt", "root", "111111");
            Statement s = co.createStatement();
            ResultSet result = s.executeQuery("select *from evaluate where 学号='" + a + "'");
            result.next();
            String ss=result.getString("姓名");
            jf.setTitle("欢迎"+ss+"来到评教系统");
            result.close();
            s.close();
            co.close();
            tj.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JButton cx = new JButton("查看评教结果");
                    jf.add(cx);
                    String ja = "";
                    String py = "";
                    String c = "";
                    //通过面板属性名获取到该面板上的所有组件
                    for (Component y : x.getComponents()) {
                        if (y instanceof JRadioButton) {
                            if (((JRadioButton) y).isSelected()) {
                                ja += ((JRadioButton) y).getText();
                            }
                        }
                    }
                    for (Component y : x2.getComponents()) {
                        if (y instanceof JRadioButton) {
                            if (((JRadioButton) y).isSelected()) {
                                py += ((JRadioButton) y).getText();
                            }
                        }
                    }
                    for (Component y : x3.getComponents()) {
                        if (y instanceof JRadioButton) {
                            if (((JRadioButton) y).isSelected()) {
                                c += ((JRadioButton) y).getText();
                            }
                        }
                    }
                    System.out.println(ja);//输出选择的单选框文本
                    System.out.println(py);//输出选择的单选框文本
                    System.out.println(c);//输出选择的单选框文本
                    try {
                        Class.forName("com.mysql.cj.jdbc.Driver");
                        Connection co = DriverManager.getConnection("jdbc:mysql://localhost:3306/lxt", "root", "111111");
                        Statement s = co.createStatement();
                        ResultSet result = s.executeQuery("select *from evaluate where 学号='" + a + "'");
                        result.next();
                        String ss = result.getString("面向对象");


                        System.out.println(ss);
                        ts.setForeground(Color.red);
                        if (ss == null) {

                            int r = s.executeUpdate("UPDATE evaluate SET 面向对象 = '" + ja + "', c语言 = '" + c + "', py程序设计 = '" + py + "'" +
                                    "WHERE 学号 = '" + a + "'");
                            ts.setText("评教成功");
                        } else {
                            ts.setText("你已经参与过评教,无需再次评教");
                        }
                        System.out.println(a);

                        cx.addActionListener(new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                new After(a);
                            }
                        });

                        s.close();
                        co.close();

                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    } catch (ClassNotFoundException classNotFoundException) {
                        classNotFoundException.printStackTrace();
                    }
                }
            });
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
//    String pj1 = bg.isSelected(y.getModel()) ? y.getText() : yi.getText();
//    public static void main(String[] args) {
//        new xsDengLu();
//
//    }

    }
}
